﻿<?php

class Mtacpham extends CI_Model {

    private $matp;
    private $Status_matp;
    private $tentp;
    private $Status_tentp;
    private $hinhanh;
    private $Status_hinhanh;
    private $matl;
    private $Status_matl;
    private $matg;
    private $Status_matg;
    private $namxuatban;
    private $Status_namxuatban;
    private $manxb;
    private $Status_manxb;
    private $sotrang;
    private $Status_sotrang;
    private $caption;
    private $Status_caption;
    private $giagoc;
    private $Status_giagoc;
    private $giaban;
    private $Status_giaban;

    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->Oncreate();
    }

    private function Oncreate() {
        $this->matp = "";
        $this->Status_matp = 0;
        $this->tentp = "";
        $this->Status_tentp = 0;
        $this->hinhanh = "";
        $this->Status_hinhanh = 0;
        $this->matl = "";
        $this->Status_matl = 0;
        $this->matg = "";
        $this->Status_matg = 0;
        $this->namxuatban = "";
        $this->Status_namxuatban = 0;
        $this->manxb = "";
        $this->Status_manxb = 0;
        $this->sotrang = "";
        $this->Status_sotrang = 0;
        $this->caption = "";
        $this->Status_caption = 0;
        $this->giagoc = "";
        $this->Status_giagoc = 0;
        $this->giaban = "";
        $this->Status_giaban = 0;
    }

    public function set_matp($data) {
        $this->matp = $data;
        $this->Status_matp = 1;
    }

    public function set_tentp($data) {
        $this->tentp = $data;
        $this->Status_tentp = 1;
    }

    public function set_hinhanh($data) {
        $this->hinhanh = $data;
        $this->Status_hinhanh = 1;
    }

    public function set_matl($data) {
        $this->matl = $data;
        $this->Status_matl = 1;
    }

    public function set_matg($data) {
        $this->matg = $data;
        $this->Status_matg = 1;
    }

    public function set_namxuatban($data) {
        $this->namxuatban = $data;
        $this->Status_namxuatban = 1;
    }

    public function set_manxb($data) {
        $this->manxb = $data;
        $this->Status_manxb = 1;
    }

    public function set_sotrang($data) {
        $this->sotrang = $data;
        $this->Status_sotrang = 1;
    }

    public function set_caption($data) {
        $this->caption = $data;
        $this->Status_caption = 1;
    }

    public function set_giagoc($data) {
        $this->giagoc = $data;
        $this->Status_giagoc = 1;
    }

    public function set_giaban($data) {
        $this->giaban = $data;
        $this->Status_giaban = 1;
    }

    public function get_matp() {
        return $this->matp;
    }

    public function get_tentp() {
        return $this->tentp;
    }

    public function get_hinhanh_img() {
        return base_url("index.php/product/getImage/".$this->matp) ;
    }
    public function get_hinhanh() {
        return $this->hinhanh ;
    }

    public function get_matl() {
        return $this->matl;
    }

    public function get_matg() {
        return $this->matg;
    }

    public function get_namxuatban() {
        return $this->namxuatban;
    }

    public function get_manxb() {
        return $this->manxb;
    }

    public function get_sotrang() {
        return $this->sotrang;
    }

    public function get_caption() {
        return $this->caption;
    }

    public function get_giagoc() {
        return $this->giagoc;
    }

    public function get_giaban() {
        return $this->giaban;
    }

    /**
     *
     * Get  Oject
     *
     * @strWhere    Mệnh đề where
     * @return     no return. đưa các giá trị vào object gọi hàm()
     *
     */
    public function getObject($strWhere) {
        $this->Oncreate();
        $this->db->where($strWhere, NULL, FALSE);
        $query = $this->db->get("tacpham");
        $ar = $query->row_array();
        if (!empty($ar)) {
            $this->matp = $ar["MATP"];
            $this->tentp = $ar["TENTP"];
            $this->hinhanh = $ar["HINHANH"];
            $this->matl = $ar["MATL"];
            $this->matg = $ar["MATG"];
            $this->namxuatban = $ar["NAMXUATBAN"];
            $this->manxb = $ar["MANXB"];
            $this->sotrang = $ar["SOTRANG"];
            $this->caption = $ar["CAPTION"];
            $this->giagoc = $ar["GIAGOC"];
            $this->giaban = $ar["GIABAN"];
        }
    }

    /**
     *
     * Insert Object into database
     *
     */
    public function Save() {
        //$data = array("matp" => $this->matp, "tentp" => $this->tentp, "hinhanh" => $this->hinhanh, "matl" => $this->matl, "matg" => $this->matg, "namxuatban" => $this->namxuatban, "manxb" => $this->manxb, "sotrang" => $this->sotrang, "caption" => $this->caption, "giagoc" => $this->giagoc, "giaban" => $this->giaban,"sachmoi"=>1,"sachoibat"=>0);
          $data = array("matp" => $this->matp,
              "tentp" => $this->tentp,
              "hinhanh" => $this->hinhanh,
              "matl" => $this->matl, 
              "matg" => $this->matg, 
              "namxuatban" => $this->namxuatban,
              "manxb" => $this->manxb,
              "sotrang" => $this->sotrang,
              "caption" => $this->caption,
              "giagoc" => $this->giagoc,
              "giaban" => $this->giaban,
              "sachmoi"=>"1",
              "sachnoibat"=>"1");
          //return $data;
       return $this->db->insert("tacpham", $data);
    }

    /**
     *
     * Update Object có các field sử dụng hàm set_()
     *
     * @strWhere=null update tất cả các row trong database
     *
     */
    public function UpdateField($strWhere = NULL) {
        $arr = array();
        if ($this->Status_matp == 1)
            $arr["matp"] = $this->matp;
        if ($this->Status_tentp == 1)
            $arr["tentp"] = $this->tentp;
        if ($this->Status_hinhanh == 1)
            $arr["hinhanh"] = $this->hinhanh;
        if ($this->Status_matl == 1)
            $arr["matl"] = $this->matl;
        if ($this->Status_matg == 1)
            $arr["matg"] = $this->matg;
        if ($this->Status_namxuatban == 1)
            $arr["namxuatban"] = $this->namxuatban;
        if ($this->Status_manxb == 1)
            $arr["manxb"] = $this->manxb;
        if ($this->Status_sotrang == 1)
            $arr["sotrang"] = $this->sotrang;
        if ($this->Status_caption == 1)
            $arr["caption"] = $this->caption;
        if ($this->Status_giagoc == 1)
            $arr["giagoc"] = $this->giagoc;
        if ($this->Status_giaban == 1)
            $arr["giaban"] = $this->giaban;
        if (!empty($strWhere)) {
            $this->db->where($strWhere, NULL, FALSE);
        }
        $this->db->update("tacpham", $arr);
    }

    /**
     *
     * Get list Oject
     *
     * @strFieldName         Các field cần lấy dữ liệu
     * @strWhere             Mệnh đề where
     * @strOrderby           Ex: 'title desc, name asc'
     * @Limit_Number_Row     Số dòng dữ liệu cần lấy ra
     * @Limit_Start_Index    Vị trí bắt đầu lấy dữ liệu
     *     foreach ($t as $value) {
      $ob=(object)($value);
      echo $ob->tacphamname;
      echo $ob->pass;
      }
     * @return               result_object();
     *
     */
    public function getListObject($strFieldName = NULL, $strWhere = NULL, $strOrderby = NULL, $Limit_Number_Row = NULL, $Limit_Start_Index = NULL) {
        if (!is_null($strWhere)) {
            $this->db->where($strWhere, NULL, FALSE);
        }
        if (!is_null($strFieldName)) {
            $this->db->select($strFieldName);
        }
        if (!is_null($strOrderby)) {
            $this->db->order_by($strOrderby);
        }
        if (!is_null($Limit_Number_Row)) {
            if (!is_null($Limit_Start_Index))
                $this->db->limit($Limit_Number_Row, $Limit_Start_Index);
            else {
                $this->db->limit($Limit_Number_Row);
            }
        }
        $query = $this->db->get("tacpham");
        return $query->result_object();
    }
       /**
     *
     * Lây max giá trị kiểu int của 1 trường dữ liệu
     *    
     * @return          ID lớn nhất c
     *
     */
    public function GetLastID(){
        $data=$this->db->select('MATP')->order_by('MATP','DESC')->get('TACPHAM', 1);
        $data=$data->row_array();
        return $data['MATP'];
    }


    /**
     *
     * Get list Oject
     *
     * @strWhere    Mệnh đề where. Null sẽ xóa hết dữ liệu của table
     * @return      no
     *
     */
    public function Delete($strWhere = NULL) {
        if (!is_null($strWhere)) {
            $this->db->where($strWhere, NULL, FALSE);
           return $this->db->delete("tacpham");
        }
        else
            $this->db->empty_table('tacpham');
    }

    /**
     *
     * Lây max giá trị kiểu int của 1 trường dữ liệu
     *
     * @strFieldName    Trường dữ liệu cần lấy max(1 trường duy nhất)
     * @strWhere        Mệnh đề where
     * @return          1 giá trị kiểu string
     *
     */
    public function Max($strFieldName, $strWhere = NULL) {
        if (!is_null($strWhere))
            $this->db->where($strWhere, NULL, FALSE);
        $this->db->select_max($strFieldName);
        $query = $this->db->get('tacpham');
        $ob = $query->row_array();
        return $ob[$strFieldName];
    }

}
?>